#include <stdlib.h>
#include <errno.h>
#include <nxos.h>
#include "libc.h"

int system(const char *cmd)
{
    NX_Solt solt;
    char appPath[NX_FILE_MAX_NAME] = {0};
    char *spacePos;
    char * cmdline = (char *)cmd;
    NX_U32 exitCode;

    if (!cmdline)
    {
        return -1;
    }

    while (*cmdline == ' ') /* skip space in the head */
    {
        cmdline++;
    }
    
    /* get app name */
    spacePos = NX_StrChr(cmdline, ' ');
    if (spacePos == NX_NULL) /* no space, only app name */
    {
        NX_StrCopy(appPath, cmdline);
    }
    else
    {
        NX_StrCopyN(appPath, cmdline, spacePos - cmdline + 1);
    }

	solt = NX_ProcessLaunch(appPath, NX_THREAD_CREATE_WAIT, &exitCode, cmdline, libc.envline);
    if (solt == NX_SOLT_INVALID_VALUE)
    {
        return -1;
    }

	return 0;
}
